$(document).ready(
		function() {

			$('#tabs a').click(function(e) {
				e.preventDefault();
				$(this).tab('show');
			});

			chrome.extension.onConnect.addListener(function(port) {
				var tab = port.sender.tab;
				// This will get called by the content script we execute in
				// the tab as a result of the user pressing the browser action.
				port.onMessage.addListener(function(page) {
					console.debug(page.content);

					if (page.description) {
						$("#link_description").val(page.description);
					}

					if (page.content) {
						$("#bookmark_form").append(
								"<input type='hidden' name='content' value='"
										+ page.content + "'/>");
					}

					if (page.keywords) {
						$("#bookmark_form").append(
								"<input type='hidden' name='keywords' value='"
										+ page.keywords + "'/>");
					}
					if (page.keywords) {
						$("#link_tags").val(page.keywords);
					}

					$('#link_tags').tagsInput({
						width : 'auto',
						height : 30,
						'defaultText' : '+标签'
					});

				});
			});

			chrome.tabs.getSelected(null, function(tab) {
				$("#link_url").val(tab.url);
				$("#link_url_display").html(tab.url);
				$("#link_title").val(tab.title);

				chrome.tabs.executeScript(tab.id, {
					file : "jquery.min.js"
				}, function() {
					chrome.tabs.executeScript(tab.id, {
						file : "page.js"
					}, function() {
					});
				});
			});

		});
